package com.student.student.controller;

import com.student.common.entity.Result;
import com.student.common.entity.Student;
import com.student.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * 学生控制器
 * 
 * @author student
 * @date 2025
 */
@RestController
@RequestMapping("/student")
@CrossOrigin(origins = "*")
public class StudentController {
    
    @Autowired
    private StudentService studentService;
    
    /**
     * 学生列表查询
     */
    @GetMapping("/list")
    public Result<Page<Student>> getStudentList(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String studentId,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String major,
            @RequestParam(required = false) String className,
            @RequestParam(required = false) String status) {
        try {
            Pageable pageable = PageRequest.of(page, size);
            Page<Student> students = studentService.getStudentList(pageable, studentId, name, major, className, status);
            return Result.success(students);
        } catch (Exception e) {
            return Result.error("查询学生列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询学生
     */
    @GetMapping("/{id}")
    public Result<Student> getStudentById(@PathVariable String id) {
        try {
            Student student = studentService.getStudentById(id);
            if (student != null) {
                return Result.success(student);
            } else {
                return Result.error("学生不存在");
            }
        } catch (Exception e) {
            return Result.error("查询学生失败：" + e.getMessage());
        }
    }
    
    /**
     * 新增学生
     */
    @PostMapping
    public Result<String> addStudent(@RequestBody Student student) {
        try {
            boolean success = studentService.addStudent(student);
            if (success) {
                return Result.success("新增学生成功");
            } else {
                return Result.error("新增学生失败，学号已存在");
            }
        } catch (Exception e) {
            return Result.error("新增学生失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新学生信息
     */
    @PutMapping("/{id}")
    public Result<String> updateStudent(@PathVariable String id, @RequestBody Student student) {
        try {
            student.setStudentId(id);
            boolean success = studentService.updateStudent(student);
            if (success) {
                return Result.success("更新学生信息成功");
            } else {
                return Result.error("更新学生信息失败，学生不存在");
            }
        } catch (Exception e) {
            return Result.error("更新学生信息失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除学生
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteStudent(@PathVariable String id) {
        try {
            boolean success = studentService.deleteStudent(id);
            if (success) {
                return Result.success("删除学生成功");
            } else {
                return Result.error("删除学生失败，学生不存在或有选课记录");
            }
        } catch (Exception e) {
            return Result.error("删除学生失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量删除学生
     */
    @DeleteMapping("/batch")
    public Result<String> batchDeleteStudents(@RequestBody java.util.List<String> ids) {
        try {
            int successCount = 0;
            int failCount = 0;
            
            for (String id : ids) {
                boolean success = studentService.deleteStudent(id);
                if (success) {
                    successCount++;
                } else {
                    failCount++;
                }
            }
            
            if (failCount == 0) {
                return Result.success("批量删除成功，共删除 " + successCount + " 条记录");
            } else if (successCount == 0) {
                return Result.error("批量删除失败，所有记录都无法删除");
            } else {
                return Result.success("批量删除完成，成功删除 " + successCount + " 条，失败 " + failCount + " 条");
            }
        } catch (Exception e) {
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取学生总数
     */
    @GetMapping("/count")
    public Result<Long> getStudentCount() {
        try {
            long count = studentService.getStudentCount();
            return Result.success(count);
        } catch (Exception e) {
            return Result.error("获取学生总数失败：" + e.getMessage());
        }
    }
}
